import Vue from 'vue'
import Vuex from 'vuex'
import { getUserFollowList } from '../api/user.js'
import { getNewsArticleDetail } from '../api/news.js'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    isLogin: false,
    searchList: '',
    // 用户关注列表
    followList: '',
    followIdList: [],
    currentArt: ''
  },
  mutations: {
    setIsLogin (state, payload) {
      state.isLogin = payload
    },
    setSearchList (state, payload) {
      state.searchList = payload
    },
    setFollowList (state, payload) {
      state.followList = payload
    },
    setFollowIdList (state, payload) {
      state.followIdList = payload
    },
    setCurrentArt (state, payload) {
      state.currentArt = payload
    }
  },
  actions: {
    async setFlIdList (context) {
      const res = await getUserFollowList()
      console.log(res)
      const flId = []
      for (let i = 0; i < res.data.data.results.length; i++) {
        flId.push(res.data.data.results[i].id)
      }
      context.commit('setFollowIdList', flId)
    },
    async getCurArt (context, num) {
      const res = await getNewsArticleDetail(num)
      context.commit('setCurrentArt', res.data.data)
      console.log(res)
    }
  },
  getters: {
    getMyprop: function (state) {
      return function (id) {
        return state.followIdList && state.followIdList.includes(id)
      }
    },
    getArtStatus: function (state) {
      return function (status) {
        return state.currentArt && state.currentArt.attitude === status
      }
    },
    getArtCollect: function (state) {
      return function () {
        return state.currentArt && state.currentArt.is_collected
      }
    }
  },
  modules: {}
})
